package com.dd.admin.business.sellPay.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.admin.business.sell.entity.Sell;
import com.dd.admin.common.model.PageFactory;
import com.dd.admin.business.sellPay.entity.Sellpay;
import com.dd.admin.business.sellPay.mapper.SellpayMapper;
import com.dd.admin.business.sellPay.service.SellpayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.admin.common.security.SecurityUtil;
import com.dd.admin.common.utils.BigDecimalUtil;
import org.springframework.stereotype.Service;
import com.dd.admin.business.sellPay.domain.SellpayVo;
import com.dd.admin.business.sellPay.domain.SellpayDto;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import static com.dd.admin.business.pay.enmus.PayConst.PAY_TYPE_DISCOUNT;

/**
 * <p>
 * 订单支付 服务实现类
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-06-19
 */
@Service
public class SellpayServiceImpl extends ServiceImpl<SellpayMapper, Sellpay> implements SellpayService {

    @Override
    public IPage<SellpayVo> selectSellpayPage(SellpayDto sellPayDto) {
        Page page = PageFactory.defaultPage();
        return baseMapper.selectSellpayPage(page,sellPayDto);
    }

    @Override
    public List<SellpayVo> selectSellpayList(SellpayDto sellPayDto) {
        return baseMapper.selectSellpayList(sellPayDto);
    }

    @Override
    public void addSellPayList(List<Sellpay> sellPayList, String sellId) {
        // 使用stream的filter操作进行过滤，只保留amount大于0的元素
        List<Sellpay> filteredList = sellPayList.stream()
                .filter(sellpay -> sellpay.getAmount().compareTo(BigDecimal.ZERO) > 0)
                .collect(Collectors.toList());

        // 对过滤后的列表进行操作，设置sellId
        filteredList.stream().forEach(sellpay -> sellpay.setSellId(sellId));

        saveBatch(filteredList);
    }

    @Override
    public BigDecimal getRealAmount(List<Sellpay> sellPayList, BigDecimal totalAmount) {
        BigDecimal discountAmount = sellPayList.stream()
                                    .filter(sellpay -> sellpay.getPayType().equals(PAY_TYPE_DISCOUNT))
                                    .findFirst()
                                    .map(Sellpay::getAmount)
                                    .orElse(BigDecimal.ZERO);
        return BigDecimalUtil.sub(totalAmount,discountAmount);
    }

    @Override
    public List<Sellpay> selectSellPayList(List<String> sellIds) {
        // 使用 MyBatis Plus 的条件构造器查询 SellpayVo 列表
        LambdaQueryWrapper<Sellpay> sellpayQueryWrapper = new LambdaQueryWrapper<>();
        if(CollectionUtil.isNotEmpty(sellIds)){
            sellpayQueryWrapper.in(Sellpay::getSellId, sellIds);
        }
        sellpayQueryWrapper.orderByAsc(Sellpay::getPayType);
        List<Sellpay> sellpayList = list(sellpayQueryWrapper);
        return sellpayList;
    }

    @Override
    public List<Sellpay> selectSellPayList(String sellId) {
        LambdaQueryWrapper<Sellpay> sellpayQueryWrapper = new LambdaQueryWrapper<>();
        sellpayQueryWrapper.eq(Sellpay::getSellId, sellId);
        List<Sellpay> sellpayList = list(sellpayQueryWrapper);
        return sellpayList;
    }
}
